package com.guanyong.admin.controllers.api;

import com.guanyong.admin.handle.TokenHelper;
import com.guanyong.admin.handle.UploadFileType;
import com.guanyong.admin.handle.UserHandle;
import com.guanyong.model.UserBaseModel;
import com.guanyong.model.UserInfoModel;
import com.guanyong.services.UserBaseService;
import com.guanyong.services.UserInfoService;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import com.guanyong.utils.NetUtils.UploadFileUtil;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.IntrospectionException;
import java.util.HashMap;

/**
 * Created by mxk on 2017/5/29.
 */
@RestController
@RequestMapping("/api/user/")
public class UserApi {

    @Autowired
    UserInfoService userInfoService;


    @Autowired
    UserBaseService userBaseService;

    @Autowired
    UserHandle userHandle;

    @Autowired
    UploadFileUtil uploadFileUtil;

    @Autowired
    TokenHelper tokenHelper;

    @RequestMapping(value = "getuserlist", method = RequestMethod.GET)
    ResultObject<HashMap<String, Object>> getuserlist(HttpServletRequest request,
                                                      @RequestParam String phone,
                                                      @RequestParam String username,
                                                      @RequestParam Integer state,
                                                      @RequestParam Integer regapp,
                                                      @RequestParam Integer isvip,
                                                      @RequestParam String btime,
                                                      @RequestParam String etime,
                                                      @RequestParam int pno,
                                                      @RequestParam int pagesize,
                                                      @RequestParam int allcount) {
        ResultObject<HashMap<String, Object>> result = new ResultObject<HashMap<String, Object>>();
        UserBaseModel userBaseModel = new UserBaseModel();
        userBaseModel.setfRegapp(regapp);
        userBaseModel.setfState(state);
        userBaseModel.setfPhone(phone);
        userBaseModel.setfUsername(username);
        HashMap<String, Object> hashMap =
                userInfoService.GetUserList(userBaseModel, isvip, btime, etime, pno,
                        pagesize, allcount);
        result.setResult(hashMap);
        return result;
    }

    //修改用户
    @RequestMapping(value = "updateuserstate", method = RequestMethod.POST)
    ResultObject<Integer> getuserlist(HttpServletRequest request,
                                      @RequestParam String userid,
                                      @RequestParam Integer state) {
        return userBaseService.updateState(userid, state);
    }


    @RequestMapping(value = "updatepwd", method = RequestMethod.POST)
    ResultObject<Integer> updatepwd(HttpServletRequest request,
                                    @RequestParam String input_oldpwd,
                                    @RequestParam String input_newpwd1,
                                    @RequestParam String input_newpwd2
    ) {
        ResultObject<Integer> resultObject = new ResultObject<Integer>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        if (userBaseModel != null) {
            if (input_newpwd1.equals(input_newpwd2)) {
                resultObject = userBaseService.updatePwd(userBaseModel.getfId(), input_oldpwd,
                        input_newpwd1);
            } else {
                resultObject.setCode(ResultCode.CehckCodeError);
            }

        } else {

            resultObject.setCode(ResultCode.NoLogin);

        }
        return resultObject;
    }
    @RequestMapping(value = "loginout", method = RequestMethod.POST)
    public ResultObject<Integer> LoginOut(HttpServletRequest request,
                                          HttpServletResponse reponse){
        ResultObject<Integer> result = new ResultObject<Integer>();
        tokenHelper.delMyCasToken(request,reponse);
        return result;
    }

    @RequestMapping(value = "modifyUserPhoto", method = RequestMethod.POST)
    ResultObject<Integer> modifyUserPhoto(@RequestParam String data, HttpServletRequest request) {
        ResultObject<Integer> result = new ResultObject<Integer>();
        UserBaseModel user = userHandle.getCurUser(request);
        if (user != null) {
            String path = "";
            try {
                path = uploadFileUtil.SaveStringFile(data, UploadFileType.Photo, ".jpg", request);
            } catch (Exception e) {

            }
            if (!path.equals("")) {

                result = userBaseService.updatePhoto(user.getfId(), path);

            } else {
                result.setCode(ResultCode.OperateFail);
            }
        } else {

            result.setCode(ResultCode.NoLogin);

        }
        return result;
    }
}
